package main;

import ilog.concert.IloException;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import solver.Solver;
import solverdecisionrules.DRBLSC;
import solverdecisionrules.DRBLSU;
import solverdecisionrules.DRLSC;
import solverdecisionrules.DRLSU;
import tools.*;
import uncertainty.*;
import problem.*;

public class MainWareHouseProblemDR {

	public static void main(String[] args){
		UncertaintySet Xi;
		SingleWareHouseProblem problem;
		Vector<Double> demandcost;
		Vector<Double> storagecost;
		Vector<Double> shortagevost;
		Vector<Double> demandlimits;
		int k = Integer.parseInt(args[0]);
		int algo = Integer.parseInt(args[1]);
		int dim= Integer.parseInt(args[2]);
		int gamma= Integer.parseInt(args[3]);
		int numvert;
		double solution;
		double time;
		
		try {
			FileReader intancestream = new FileReader(args[4]);
			FileReader ustream = new FileReader(args[5]);
			FileWriter file = new FileWriter(args[6], true);
			double initstock = Integer.parseInt(args[7]);
			
			BufferedReader intsFile = new BufferedReader(intancestream);
			BufferedReader usFile = new BufferedReader(ustream);
			
			demandlimits = MainFunctions.readBounds(intsFile);
			demandcost = MainFunctions.readDemandCosts(intsFile);
			storagecost = MainFunctions.readStorageCosts(intsFile);
			shortagevost = MainFunctions.readShortageCosts(intsFile);
			
			
			Xi = MainFunctions.readUncertainSet(gamma, usFile);
			problem = new SingleWareHouseProblem(Xi, dim, k, demandcost, storagecost, shortagevost, demandlimits, initstock);
			Solver solver; 
			
			
			switch(algo){
			case 1: solver = new DRLSU(problem, Xi ,k);
				break;
			case 2:  solver = new DRLSC(problem, Xi ,k);
				break;
			case 3:  solver = new DRBLSU(problem, Xi ,k);
				break;
			case 4:  solver = new DRBLSC(problem, Xi ,k);
				break;
			default: solver = new DRLSU(problem, Xi ,k);
				break;	
			}
			
			
			solver.build();
			
			solution = solver.solve();
			
			numvert = Xi.getNumVert();
			
			time = solver.getTime();
			
			MainFunctions.writeSol(file, args[5], dim, k, gamma, numvert, args[4], solution, time);
			file.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IloException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
